<template>
	<view>
		<view class="header">
			<image :src="userInfo.avatar" mode=""></image>
			<view class="nikename">{{userInfo.nickname}}</view>
		</view>
		<view class="content">
			<van-cell title="会员中心" is-link />
			<van-cell title="我的订单" is-link />
			<van-cell title="收货地址" is-link />
			<van-cell title="我的足迹" is-link />
			<van-cell title="我的收藏" is-link />
			<van-cell title="意见反馈" is-link />
		</view>
		<view class="footer">
			<van-button type="primary" block round @click="logout">退出</van-button>
		</view>
		<van-dialog id="van-dialog" />
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { getUserInfo } from '../../api/user';
	import { useTabBarBadge, useSetTabBarItem, useIsToken } from '../../hooks/index'
	import { useUserStore } from '../../stores/user';
	import { UserInfo } from "../../types/user"
	import Dialog from '/wxcomponents/vant/dialog/dialog';

	useTabBarBadge()
	useIsToken()
	useSetTabBarItem()
	const store = useUserStore()

	const userInfo = ref({} as UserInfo)

	const getUserInfoData = async () => {
		const res = await getUserInfo()
		if (res.code === 200) {
			userInfo.value = res.data
		}
	}

	getUserInfoData()

	const logout = () => {
		Dialog.confirm({
			title: '提示',
			message: '你确认退出吗?',
		})
			.then(() => {
				store.setToken('')
				uni.setTabBarItem({
					index: 3,
					text: '未登录'
				})
				uni.redirectTo({
					url: '/subpkg/login/login'
				})
			})
	}
</script>

<style lang="scss" scoped>
	.header {
		height: 400rpx;
		background-color: #c82519;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		image {
			width: 250rpx;
			height: 250rpx;
			border-radius: 50%;
		}

		.nikename {
			color: white;
			font-size: 24px;
			margin-top: 10px;
		}
	}

	.footer {
		margin: 20px 20px;
	}
</style>